using System;
using System.Collections.Generic;
using System.Text;
using THSLib;
using THSERP.BusinessEntities;
using THSERP.BaseProvider;

namespace THSERP.Modules.InvoiceIn
{
    public class InvoiceInEntities : ERPModuleEntities 
    {
        #region Declare Constant
        public readonly static String strAPInvoiceInObjectName = "APInvoiceIns";

        public readonly static String strAPInvoiceInItemsObjectName = "APInvoiceInItems";
        #endregion

        #region Declare all entities variables
        private APInvoiceInsInfo _aPInvoiceInsObject;
        private APInvoiceInItemsInfo _aPInvoiceInItemsObject;

        public THSItemsEntityList<InvoiceInItemsEntity> lstAPInvoiceInItems;
        #endregion

        #region Public Properties
        public APInvoiceInsInfo APInvoiceInsObject
        {
            get
            {
                return _aPInvoiceInsObject;
            }
            set
            {
                _aPInvoiceInsObject = value;
            }
        }
        public APInvoiceInItemsInfo APInvoiceInItemsObject
        {
            get
            {
                return _aPInvoiceInItemsObject;
            }
            set
            {
                _aPInvoiceInItemsObject = value;
            }
        }

        public THSItemsEntityList<InvoiceInItemsEntity> APInvoiceInItemsList
        {
            get
            {
                return lstAPInvoiceInItems;
            }
            set
            {
                lstAPInvoiceInItems = value;
            }
        }

        #endregion

        #region Constructor
        public InvoiceInEntities()
            : base()
        {
            _aPInvoiceInsObject = new APInvoiceInsInfo();

            _aPInvoiceInItemsObject = new APInvoiceInItemsInfo();

            lstAPInvoiceInItems = new THSItemsEntityList<InvoiceInItemsEntity>();
        }

        #endregion

        #region Init Main Object,Module Objects functions
        public override void InitMainObject()
        {

            MainObject = APInvoiceInsObject;
        }

        public override void InitModuleObjects()
        {
            ModuleObjects.Add("APInvoiceInItems", APInvoiceInItemsObject);
        }

        public override void InitModuleObjectList()
        {
            lstAPInvoiceInItems = new THSItemsEntityList<InvoiceInItemsEntity>();
            lstAPInvoiceInItems.InitTHSList(this,
                                             THSUtil.GetTableNameFromBusinessObjectType(typeof(APInvoiceInsInfo)),
                                             THSUtil.GetTableNameFromBusinessObjectType(typeof(APInvoiceInItemsInfo)));
        }

        public override void InitGridControlInTHSList()
        {
            lstAPInvoiceInItems.InitTHSListGridControl();
        }

        public override void SetDefaultModuleObjectsList()
        {
            try
            {
                lstAPInvoiceInItems.SetDefaultListAndRefreshGridControl();

            }
            catch (Exception)
            {
                return;
            }
        }
        public override double CalculateProductPrice(int iICProductID, DateTime dtTransactionDate, double dbProductQty, int iGECurrencyID)
        {
            //return base.CalculateProductPrice(iICProductID, dtTransactionDate, dbProductQty, iGECurrencyID);
            double dbProductUnitPrice=0;
            try
            {
                

                String strItemProductUnitPriceColumn = strAPInvoiceInItemsObjectName.Substring(strAPInvoiceInItemsObjectName.Length - 1) + "ProductUnitPrice";
                dbProductUnitPrice = Convert.ToDouble(THSDbUtil.GetPropertyValue(ModuleObjects[strAPInvoiceInItemsObjectName], strItemProductUnitPriceColumn));
            }
            catch (Exception)
            {
                dbProductUnitPrice = 0;
            }
            return dbProductUnitPrice;
            
        }

        #endregion

        #region Invalidate Module Objects Functions
        public override void InvalidateModuleObjects(int iObjectID)
        {
            APInvoiceInItemsList.Invalidate(iObjectID);
        }
        #endregion

        #region Save Module Objects Functions
        public override void SaveModuleObjects()
        {           
            APInvoiceInItemsList.SaveItemObjects(true);
        }
        #endregion

        public override void SetValuesAfterValidateProduct(int iICProductID)
        {
            APInvoiceInItemsList.SetDefaultValuesFromProduct(iICProductID);

            //Set Default Stock is Main Object Stock
            ((APInvoiceInItemsInfo)ModuleObjects[strAPInvoiceInItemsObjectName]).FK_ICStockID = ((APInvoiceInsInfo)MainObject).FK_ICStockID;
            UpdateModuleObjectBindingSource(strAPInvoiceInItemsObjectName);

            
        }   
    }
}
